package org.chnxi.study.springcloud.dubbo.shiro.mybatisplus;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Scanner;

public class CodeGenerator {

    public static String scanner(String tip){
        Scanner scanner = new Scanner(System.in);
        System.out.println(tip);
        if (scanner.hasNext()){
            String value = scanner.next();
            if(StringUtils.isNotBlank(value)){
                return value;
            }
        }
        throw new MybatisPlusException("输出错误："+tip);
    }

    static String getPrjPath(){
        String prjPath = System.getProperty("user.dir");
        for (;;){
            String result = scanner("请确认项目路径(Y/N)："+prjPath);
            if("n".equalsIgnoreCase(result)){
                prjPath = scanner("请输出新的项目路径：");
            }else if("y".equalsIgnoreCase(result)){
                return prjPath;
            }
        }
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        final String prjPath = getPrjPath();

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(prjPath+"/src/main/java");
        globalConfig.setAuthor("");  //????
        globalConfig.setOpen(Boolean.FALSE);
        globalConfig.setSwagger2(Boolean.TRUE);
        autoGenerator.setGlobalConfig(globalConfig);

        // 配合数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://192.168.50.140/shiro?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        autoGenerator.setDataSource(dataSourceConfig);

        // 包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName(scanner("模块名："));
        packageConfig.setParent("org.chnxi.study.springcloud.dubbo.shiro");
        autoGenerator.setPackageInfo(packageConfig);

        // 自定义配置
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };

        //使用freemarker作为模板引擎
        String templatePath = "/templates/mapper.xml.ftl";
        //使用velocity作为模板迎请
//        String templatePath = "/templates/mapper.xml.vm";

        //自定义输出配置
        ArrayList<FileOutConfig> fileOutConfigs = new ArrayList<>();

        fileOutConfigs.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名称，如果你的Entity设置了前后缀，此处的xml名称会跟着发生变化
                return prjPath+"/src/main/resources/mapper/"+packageConfig.getModuleName()+"/"
                        +tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
            }
        });

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        autoGenerator.setTemplate(templateConfig);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(Boolean.TRUE);
        strategyConfig.setRestControllerStyle(Boolean.TRUE);
        strategyConfig.setEntityColumnConstant(Boolean.TRUE);
        //strategyConfig.setSuperEntityColumns("id");
        strategyConfig.setInclude(scanner("输入多个表名，用英文\",\"分开").split(","));
        strategyConfig.setControllerMappingHyphenStyle(Boolean.TRUE);
        strategyConfig.setTablePrefix(packageConfig.getModuleName()+"_");
        autoGenerator.setStrategy(strategyConfig);

        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();

    }

}
